## Figure 1

library(dplyr)
library(ggplot2)
library(reshape2)
library(timeDate)
library(eeptools)
library(gridExtra)
library(grid)
library(rdrobust)
library(rdlocrand)
library(lubridate)

## Clear workspace

rm(list=ls())


##Opening the data:

load("ReplicationData.RData")


## 2010

## Turnout variable
data2010$turnout <- as.numeric(data2010$VOTOU.1º.TURNO) - 1

## Subsetting the 4-year window: 3 oct 1990 - 3 oct 1994

data.young.2010 <- dplyr::filter(data2010, (dob >= "1990-10-03" & dob <= "1994-10-03") & turnout == 1)

DataFigureYoung2010 <- data.young.2010 %>%
  group_by(dob) %>%
  summarise(voters = n())


## Age variable:

DataFigureYoung2010$age <- age_calc(DataFigureYoung2010$dob, enddate = as.Date("2010/10/03"), units = "years", precise = TRUE)

## Obliged variable:

DataFigureYoung2010$obliged <- as.numeric(DataFigureYoung2010$age >= 18)


## Figure 1A:

figure1A <- ggplot(DataFigureYoung2010, aes(x = age,y = voters)) +
    annotate("rect", xmin = 18, xmax = 20, ymin = -Inf, ymax = Inf, alpha = .25) +
    geom_point(aes(group = obliged),size = 0.1) +
    geom_vline(xintercept = 16, linetype = 3) +
    geom_vline(xintercept = 17, linetype = 3) +
    geom_vline(xintercept = 18) +
    annotate('text', x = 18.2,y = 1200, label = "ED", size = 3) +
    geom_vline(xintercept = 19, linetype = 3) +
    geom_vline(xintercept = 17.755, linetype = 2) +
    annotate('text', x = 17.55, y = 1200, label = "EY", size = 3) +
    ylim(1000, 8500) +
    xlab('Age at election') +
    ylab("Number of voters in 2010") +
    theme(axis.title.x = element_text(vjust = 0), axis.title.y = element_text(vjust = 0.5)) +
    annotate('text', x = 16.5,y = 6500, label = "Not \n obliged \n to vote", hjust = 0.5, size = 3) +
    annotate('text', x = 19.5,y = 4500, label = "Obliged \n to vote", hjust = 0.5, size = 3) +
    theme_bw() +
    theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
    ggtitle("A. 2010 Presidential Elections")


## 2012

## Turnout variable
data2012$turnout <- as.numeric(data2012$VOTOU.1º.TURNO) - 1

## Subsetting the 4-year window: 7 oct 1992 - 7 oct 1996

data.young.2012 <- dplyr::filter(data2012, (dob >= "1992-10-07" & dob <= "1996-10-07") & turnout == 1)

DataFigureYoung2012 <- data.young.2012 %>%
  group_by(dob) %>%
  summarise(voters = n())

## Age variable:

DataFigureYoung2012$age <- age_calc(DataFigureYoung2012$dob, enddate = as.Date("2012/10/07"), units = "years", precise = TRUE)

## Obliged variable:

DataFigureYoung2012$obliged <- as.numeric(DataFigureYoung2012$age >= 18)


## Figure 1B:

figure1B <- ggplot(DataFigureYoung2012, aes(x = age,y = voters)) +
    annotate("rect", xmin = 18, xmax = 20, ymin = -Inf, ymax = Inf, alpha = .25) +
    geom_point(aes(group = obliged),size = 0.1) +
    geom_vline(xintercept = 16, linetype = 3) +
    geom_vline(xintercept = 17, linetype = 3) +
    geom_vline(xintercept = 18) +
    annotate('text', x = 18.2,y = 1200, label = "ED", size = 3) +
    geom_vline(xintercept = 19, linetype = 3) +
    geom_vline(xintercept = 17.755, linetype = 2) +
    annotate('text', x = 17.55, y = 1200, label = "EY", size = 3) +
    ylim(1000, 8500) +
    xlab('Age at election') +
    ylab("Number of voters in 2012") +
    theme(axis.title.x = element_text(vjust = 0), axis.title.y = element_text(vjust = 0.5)) +
    annotate('text', x = 16.5,y = 6500, label = "Not \n obliged \n to vote", hjust = 0.5, size = 3) +
    annotate('text', x = 19.5,y = 4500, label = "Obliged \n to vote", hjust = 0.5, size = 3) +
    theme_bw() +
    theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
    ggtitle("B. 2012 Municipal Elections")



## Combining the 2 plots into one figure

Figure1 <- grid.arrange(figure1A, figure1B, 
              nrow = 1)

ggsave(file="Figure1.png", plot = Figure1, width = 7.5, height = 4.5)
